/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIOption.java
 *
 * Created on Apr 15, 2011, 12:51:03 PM
 */
package horsechess.client.gui;

import horsechess.client.data.CustomizeData;
import horsechess.client.handle.PlayerConnect;
import horsechess.packet.PlayerPacket;
import horsechess.server.data.ServerInfo;
import horsechess.util.Status;
import java.awt.Dialog;
import java.awt.Frame;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.Timer;

/**
 *
 * @author Administrator
 */
public class GUIOption extends javax.swing.JDialog {

    /** Creates new form GUIOption */
    public GUIOption(Frame f, String title, PlayerConnect p) {
        super(f, title);
        setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
        initComponents();
        cData = new CustomizeData();
        this.player = p;
        //System.out.println("Number Of Players: " + cData.getNumberOfPlayer());
        pnlPlayer1.setVisible(false);
        pnlPlayer2.setVisible(false);
        pnlPlayer3.setVisible(false);
        pnlPlayer4.setVisible(false);
        cData.setNumberOfPlayer(0);
        btnSend.setText("Send");
    }

    public boolean isConnect() {
        return connect;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btgDiceChosen = new javax.swing.ButtonGroup();
        grpPriority = new javax.swing.ButtonGroup();
        optionTab = new javax.swing.JTabbedPane();
        pnlConnect = new javax.swing.JPanel();
        lblServerIP1 = new javax.swing.JLabel();
        txtServerIP = new javax.swing.JTextField();
        lblServerIP2 = new javax.swing.JLabel();
        txtPlayerName = new javax.swing.JTextField();
        lblServerIP3 = new javax.swing.JLabel();
        txtServerPort = new javax.swing.JTextField();
        cboRoom = new javax.swing.JComboBox();
        lblServerIP4 = new javax.swing.JLabel();
        lblMessage = new javax.swing.JLabel();
        btnJoin = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        btnConnect = new javax.swing.JButton();
        btnDisconnect = new javax.swing.JButton();
        pnlDice = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        radOneDice = new javax.swing.JRadioButton();
        radTwoDice = new javax.swing.JRadioButton();
        jLabel8 = new javax.swing.JLabel();
        guiDice = new horsechess.client.gui.GUIDice();
        jLabel1 = new javax.swing.JLabel();
        pnlPlayer = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        pnlPlayer1 = new javax.swing.JPanel();
        lblPlayer1 = new javax.swing.JLabel();
        txtPlayerName1 = new javax.swing.JTextField();
        picChessman1 = new javax.swing.JLabel();
        radPriority1 = new javax.swing.JRadioButton();
        lblNoOfHorse1 = new javax.swing.JLabel();
        pnlPlayer2 = new javax.swing.JPanel();
        lblPlayer2 = new javax.swing.JLabel();
        txtPlayerName2 = new javax.swing.JTextField();
        picChessman2 = new javax.swing.JLabel();
        radPriority2 = new javax.swing.JRadioButton();
        lblNoOfHorse2 = new javax.swing.JLabel();
        pnlPlayer3 = new javax.swing.JPanel();
        lblPlayer3 = new javax.swing.JLabel();
        txtPlayerName3 = new javax.swing.JTextField();
        picChessman3 = new javax.swing.JLabel();
        radPriority3 = new javax.swing.JRadioButton();
        lblNoOfHorse3 = new javax.swing.JLabel();
        pnlPlayer4 = new javax.swing.JPanel();
        lblPlayer4 = new javax.swing.JLabel();
        txtPlayerName4 = new javax.swing.JTextField();
        picChessman4 = new javax.swing.JLabel();
        radPriority4 = new javax.swing.JRadioButton();
        lblNoOfHorse4 = new javax.swing.JLabel();
        txtOptionPlayer = new javax.swing.JTextField();
        pnlChat = new javax.swing.JPanel();
        scrollPane = new javax.swing.JScrollPane();
        incommingList = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtMessage = new javax.swing.JTextArea();
        btnSend = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        optionTab.setBackground(new java.awt.Color(255, 255, 255));
        optionTab.setMaximumSize(new java.awt.Dimension(450, 320));
        optionTab.setMinimumSize(new java.awt.Dimension(450, 320));
        optionTab.setPreferredSize(new java.awt.Dimension(450, 320));

        pnlConnect.setBackground(new java.awt.Color(255, 255, 255));
        pnlConnect.setLayout(null);

        lblServerIP1.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        lblServerIP1.setText("Server IP");
        pnlConnect.add(lblServerIP1);
        lblServerIP1.setBounds(20, 40, 90, 21);
        pnlConnect.add(txtServerIP);
        txtServerIP.setBounds(110, 40, 190, 20);

        lblServerIP2.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        lblServerIP2.setText("Room");
        lblServerIP2.setPreferredSize(new java.awt.Dimension(60, 14));
        pnlConnect.add(lblServerIP2);
        lblServerIP2.setBounds(70, 200, 90, 14);
        pnlConnect.add(txtPlayerName);
        txtPlayerName.setBounds(110, 100, 110, 20);

        lblServerIP3.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        lblServerIP3.setText("Server Port");
        lblServerIP3.setPreferredSize(new java.awt.Dimension(60, 14));
        pnlConnect.add(lblServerIP3);
        lblServerIP3.setBounds(20, 70, 90, 14);
        pnlConnect.add(txtServerPort);
        txtServerPort.setBounds(110, 70, 60, 20);

        cboRoom.setFont(new java.awt.Font("Calibri", 1, 16));
        cboRoom.setForeground(new java.awt.Color(0, 51, 51));
        cboRoom.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        pnlConnect.add(cboRoom);
        cboRoom.setBounds(150, 200, 150, 20);

        lblServerIP4.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        lblServerIP4.setText("Player name");
        lblServerIP4.setPreferredSize(new java.awt.Dimension(60, 14));
        pnlConnect.add(lblServerIP4);
        lblServerIP4.setBounds(20, 100, 90, 14);

        lblMessage.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblMessage.setForeground(java.awt.Color.red);
        pnlConnect.add(lblMessage);
        lblMessage.setBounds(30, 230, 400, 30);

        btnJoin.setText("Join");
        btnJoin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnJoinActionPerformed(evt);
            }
        });
        pnlConnect.add(btnJoin);
        btnJoin.setBounds(330, 200, 80, 23);

        btnRefresh.setBackground(new java.awt.Color(0, 102, 102));
        btnRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/Other/refresh.png"))); // NOI18N
        btnRefresh.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(51, 51, 0)));
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });
        pnlConnect.add(btnRefresh);
        btnRefresh.setBounds(340, 50, 80, 57);

        btnConnect.setText("Connect");
        btnConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConnectConnectActionPerformed(evt);
            }
        });
        pnlConnect.add(btnConnect);
        btnConnect.setBounds(30, 140, 170, 40);

        btnDisconnect.setText("Disconnect");
        btnDisconnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDisconnectActionPerformed(evt);
            }
        });
        pnlConnect.add(btnDisconnect);
        btnDisconnect.setBounds(230, 140, 180, 40);

        optionTab.addTab("Connection", null, pnlConnect, "Connection");

        pnlDice.setBackground(new java.awt.Color(255, 255, 255));

        jLabel7.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel7.setForeground(new java.awt.Color(0, 51, 255));
        jLabel7.setText("Dice chosen");

        radOneDice.setBackground(new java.awt.Color(255, 255, 255));
        btgDiceChosen.add(radOneDice);
        radOneDice.setFont(new java.awt.Font("Tahoma", 0, 12));
        radOneDice.setSelected(true);
        radOneDice.setText("One dice");
        radOneDice.setEnabled(false);

        radTwoDice.setBackground(new java.awt.Color(255, 255, 255));
        btgDiceChosen.add(radTwoDice);
        radTwoDice.setFont(new java.awt.Font("Tahoma", 0, 12));
        radTwoDice.setText("Two dice");
        radTwoDice.setEnabled(false);

        jLabel8.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel8.setForeground(new java.awt.Color(0, 51, 255));
        jLabel8.setText("Image demonstration");

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 18));
        jLabel1.setForeground(new java.awt.Color(0, 51, 102));
        jLabel1.setText("Dice Option");

        javax.swing.GroupLayout pnlDiceLayout = new javax.swing.GroupLayout(pnlDice);
        pnlDice.setLayout(pnlDiceLayout);
        pnlDiceLayout.setHorizontalGroup(
            pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDiceLayout.createSequentialGroup()
                .addGroup(pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlDiceLayout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(radTwoDice)
                            .addComponent(radOneDice))
                        .addGroup(pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(pnlDiceLayout.createSequentialGroup()
                                .addGap(116, 116, 116)
                                .addComponent(jLabel8))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlDiceLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(guiDice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(21, 21, 21))))
                    .addGroup(pnlDiceLayout.createSequentialGroup()
                        .addGap(156, 156, 156)
                        .addComponent(jLabel1)))
                .addContainerGap(71, Short.MAX_VALUE))
        );
        pnlDiceLayout.setVerticalGroup(
            pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDiceLayout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addGap(30, 30, 30)
                .addGroup(pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addGroup(pnlDiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlDiceLayout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(radTwoDice))
                    .addComponent(guiDice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(radOneDice))
                .addGap(144, 144, 144))
        );

        optionTab.addTab("Dice", null, pnlDice, "Dice");

        pnlPlayer.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer.setMinimumSize(new java.awt.Dimension(450, 320));

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel9.setText("Number of players");

        jLabel10.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel10.setForeground(new java.awt.Color(111, 74, 21));
        jLabel10.setText("No.");

        jLabel11.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel11.setForeground(new java.awt.Color(111, 74, 21));
        jLabel11.setText("Name");

        jLabel12.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel12.setForeground(new java.awt.Color(111, 74, 21));
        jLabel12.setText("Sample");

        jLabel13.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel13.setForeground(new java.awt.Color(111, 74, 21));
        jLabel13.setText("Number of horses");

        jLabel14.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
        jLabel14.setForeground(new java.awt.Color(111, 74, 21));
        jLabel14.setText("Priority");

        pnlPlayer1.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer1.setLayout(null);

        lblPlayer1.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblPlayer1.setText("1");
        pnlPlayer1.add(lblPlayer1);
        lblPlayer1.setBounds(0, 0, 7, 15);

        txtPlayerName1.setFont(new java.awt.Font("Verdana", 1, 12));
        txtPlayerName1.setForeground(new java.awt.Color(51, 102, 0));
        txtPlayerName1.setText("User1");
        txtPlayerName1.setEnabled(false);
        pnlPlayer1.add(txtPlayerName1);
        txtPlayerName1.setBounds(40, 0, 120, 22);

        picChessman1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/resizehorse/GREEN.gif"))); // NOI18N
        picChessman1.setMaximumSize(new java.awt.Dimension(33, 33));
        picChessman1.setMinimumSize(new java.awt.Dimension(33, 33));
        picChessman1.setPreferredSize(new java.awt.Dimension(33, 33));
        pnlPlayer1.add(picChessman1);
        picChessman1.setBounds(180, 0, 23, 30);

        radPriority1.setBackground(new java.awt.Color(255, 255, 255));
        radPriority1.setEnabled(false);
        pnlPlayer1.add(radPriority1);
        radPriority1.setBounds(390, 0, 21, 21);
        pnlPlayer1.add(lblNoOfHorse1);
        lblNoOfHorse1.setBounds(260, 0, 40, 20);

        pnlPlayer2.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer2.setLayout(null);

        lblPlayer2.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblPlayer2.setText("2");
        pnlPlayer2.add(lblPlayer2);
        lblPlayer2.setBounds(0, 0, 15, 15);

        txtPlayerName2.setFont(new java.awt.Font("Verdana", 1, 12));
        txtPlayerName2.setForeground(new java.awt.Color(51, 102, 0));
        txtPlayerName2.setText("User2");
        txtPlayerName2.setEnabled(false);
        pnlPlayer2.add(txtPlayerName2);
        txtPlayerName2.setBounds(40, 0, 120, 22);

        picChessman2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/resizehorse/RED.gif"))); // NOI18N
        pnlPlayer2.add(picChessman2);
        picChessman2.setBounds(180, 0, 20, 20);

        radPriority2.setBackground(new java.awt.Color(255, 255, 255));
        radPriority2.setEnabled(false);
        pnlPlayer2.add(radPriority2);
        radPriority2.setBounds(390, 0, 21, 21);
        pnlPlayer2.add(lblNoOfHorse2);
        lblNoOfHorse2.setBounds(260, 0, 40, 20);

        pnlPlayer3.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer3.setLayout(null);

        lblPlayer3.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblPlayer3.setText("3");
        pnlPlayer3.add(lblPlayer3);
        lblPlayer3.setBounds(0, 0, 24, 15);

        txtPlayerName3.setFont(new java.awt.Font("Verdana", 1, 12));
        txtPlayerName3.setForeground(new java.awt.Color(51, 102, 0));
        txtPlayerName3.setText("User3");
        txtPlayerName3.setEnabled(false);
        pnlPlayer3.add(txtPlayerName3);
        txtPlayerName3.setBounds(40, 0, 120, 22);

        picChessman3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/resizehorse/YELLOW.gif"))); // NOI18N
        pnlPlayer3.add(picChessman3);
        picChessman3.setBounds(180, 0, 24, 24);

        radPriority3.setBackground(new java.awt.Color(255, 255, 255));
        radPriority3.setEnabled(false);
        pnlPlayer3.add(radPriority3);
        radPriority3.setBounds(390, 0, 21, 21);
        pnlPlayer3.add(lblNoOfHorse3);
        lblNoOfHorse3.setBounds(260, 0, 40, 20);

        pnlPlayer4.setBackground(new java.awt.Color(255, 255, 255));
        pnlPlayer4.setLayout(null);

        lblPlayer4.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblPlayer4.setText("4");
        pnlPlayer4.add(lblPlayer4);
        lblPlayer4.setBounds(0, 2, 16, 15);

        txtPlayerName4.setFont(new java.awt.Font("Verdana", 1, 12));
        txtPlayerName4.setForeground(new java.awt.Color(51, 102, 0));
        txtPlayerName4.setText("User4");
        txtPlayerName4.setEnabled(false);
        pnlPlayer4.add(txtPlayerName4);
        txtPlayerName4.setBounds(40, 0, 120, 22);

        picChessman4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/resizehorse/BLUE.gif"))); // NOI18N
        pnlPlayer4.add(picChessman4);
        picChessman4.setBounds(180, 0, 24, 24);

        radPriority4.setBackground(new java.awt.Color(255, 255, 255));
        radPriority4.setEnabled(false);
        pnlPlayer4.add(radPriority4);
        radPriority4.setBounds(390, 0, 21, 21);
        pnlPlayer4.add(lblNoOfHorse4);
        lblNoOfHorse4.setBounds(260, 0, 40, 20);

        txtOptionPlayer.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
        txtOptionPlayer.setForeground(new java.awt.Color(51, 0, 51));

        javax.swing.GroupLayout pnlPlayerLayout = new javax.swing.GroupLayout(pnlPlayer);
        pnlPlayer.setLayout(pnlPlayerLayout);
        pnlPlayerLayout.setHorizontalGroup(
            pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPlayerLayout.createSequentialGroup()
                .addGroup(pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlPlayerLayout.createSequentialGroup()
                        .addGap(102, 102, 102)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtOptionPlayer, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlPlayerLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pnlPlayer1, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                            .addComponent(pnlPlayer2, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                            .addComponent(pnlPlayer3, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                            .addComponent(pnlPlayer4, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                            .addGroup(pnlPlayerLayout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel11)
                                .addGap(85, 85, 85)
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel13)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel14)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        pnlPlayerLayout.setVerticalGroup(
            pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPlayerLayout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtOptionPlayer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlPlayerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel10)
                    .addComponent(jLabel14)
                    .addComponent(jLabel11)
                    .addComponent(jLabel13)
                    .addComponent(jLabel12))
                .addGap(18, 18, 18)
                .addComponent(pnlPlayer1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlPlayer2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlPlayer3, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlPlayer4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(60, 60, 60))
        );

        optionTab.addTab("Player", pnlPlayer);

        pnlChat.setBackground(new java.awt.Color(255, 255, 255));

        scrollPane.setViewportView(incommingList);

        txtMessage.setColumns(20);
        txtMessage.setRows(5);
        jScrollPane1.setViewportView(txtMessage);

        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Comic Sans MS", 1, 18));
        jLabel2.setForeground(new java.awt.Color(102, 102, 0));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Chating");

        javax.swing.GroupLayout pnlChatLayout = new javax.swing.GroupLayout(pnlChat);
        pnlChat.setLayout(pnlChatLayout);
        pnlChatLayout.setHorizontalGroup(
            pnlChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlChatLayout.createSequentialGroup()
                .addGap(74, 74, 74)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(99, Short.MAX_VALUE))
            .addGroup(pnlChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlChatLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(pnlChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(scrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 425, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(pnlChatLayout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnSend, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap()))
        );
        pnlChatLayout.setVerticalGroup(
            pnlChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlChatLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addContainerGap(255, Short.MAX_VALUE))
            .addGroup(pnlChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlChatLayout.createSequentialGroup()
                    .addGap(56, 56, 56)
                    .addComponent(scrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(pnlChatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnSend, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE))
                    .addContainerGap()))
        );

        optionTab.addTab("Chat", pnlChat);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(optionTab, javax.swing.GroupLayout.PREFERRED_SIZE, 450, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(optionTab, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened

        guiDice.loadImageDice1(1);
        guiDice.loadImageDice2(6);
        guiDice.localizeDice();

        txtOptionPlayer.setEnabled(false);
        clearPlayer();
    }//GEN-LAST:event_formWindowOpened

    public void clearPlayer() {
        hideShowChessMan(1, false);
        hideShowChessMan(2, false);
        hideShowChessMan(3, false);
        hideShowChessMan(4, false);
    }

    public void updateDisplay() {
        //      System.out.println("Status of Server "+player.getStatus());
        if (!player.getStatus().equals(Status.EXCEPTION)) {
            serverInfo = player.getInfoServer();
            if(!txtServerIP.getText().trim().equals(serverInfo.serverAddress))
            txtServerIP.setText(serverInfo.serverAddress);
            txtServerPort.setText(serverInfo.port + "");
            //cboRoom.setModel(new DefaultComboBoxModel(serverInfo.rooms));
            btnRefresh.setEnabled(false);
        }
    }

    public void updateRooms() {
        cboRoom.setModel(new DefaultComboBoxModel(serverInfo.rooms));
    }
    private void btnDisconnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDisconnectActionPerformed
        player.requireForDisconnect();
        lblMessage.setText(player.getMessage());
        btnDisconnect.setText("Disconnected");
        btnDisconnect.setEnabled(false);
        btnConnect.setEnabled(true);
//        this.dispose();
//        GUIClient.uIClient.dispose();
//        Thread.currentThread().interrupt();
    }//GEN-LAST:event_btnDisconnectActionPerformed

    private void btnJoinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnJoinActionPerformed
        player.joinTheRoom(player.getName(), (String) cboRoom.getSelectedItem());
        lblMessage.setText(player.getName() + " has been joinned the room.");
        btnJoin.setEnabled(false);
    }//GEN-LAST:event_btnJoinActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        if (player == null) {
            System.out.println("Null roi ba con oi");
        }
        
        threads.add(player.startReceiveServer());
        if (player.infoServer != null) {
            for (int i = 0; i < threads.size(); i++) {
                Thread thread = threads.get(i);
                if(thread.isAlive())
                thread.stop();
            }
            lblMessage.setText(player.getMessage());
            if (player.getStatus().equals(Status.BEGIN_CONNECT)) {
                btnConnect.setEnabled(true);

            }
        }

    }//GEN-LAST:event_btnRefreshActionPerformed

    private void btnConnectConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectConnectActionPerformed
        if (txtPlayerName.getText().trim().equals("")) {
            lblMessage.setText("Please enter your user name!!! ");
            return;
        }
        if (!txtPlayerName.getText().trim().equals("")) {
            try {
                player.makeConnection(txtPlayerName.getText());
                GUIClient.lblPlayerName.setText(player.getName());
                lblMessage.setText(player.getMessage());
                btnDisconnect.setEnabled(true);
                btnConnect.setEnabled(false);
            }catch(Exception ex)
            {
                btnDisconnect.setEnabled(false);
                btnConnect.setEnabled(true);
            }
        }
}//GEN-LAST:event_btnConnectConnectActionPerformed

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
        if (txtMessage.getText() != null) {

            player.send("@Chat#" + txtPlayerName.getText() + ": " + txtMessage.getText());
            txtMessage.setText("");
        }
//        System.out.println("************************************************************************");
//        for (int i = 0; i < GUIClient.uIClient.cb.getInformationBoard().arrChessBoard.length; i++) {
//            int j = GUIClient.uIClient.cb.getInformationBoard().arrChessBoard[i];
//            System.out.println("Cell " + i + ":" + j);
//        }
//        System.out.println("************************************************************************");
}//GEN-LAST:event_btnSendActionPerformed
    public void disconnect() {
        JOptionPane.showMessageDialog(this, "Server is disconnected", "DISCONNECTED", JOptionPane.ERROR_MESSAGE);
    }

    /**
     * Hide/Show Chessman
     * @param n
     * @param state
     */
    public void hideShowChessMan(int n, boolean state) {
        if (n == 1) {
            if (pnlPlayer1.isVisible() != state) {
                pnlPlayer1.setVisible(state);
            }
        }
        if (n == 2) {
            if (pnlPlayer2.isVisible() != state) {
                pnlPlayer2.setVisible(state);
            }
        }
        if (n == 3) {
            if (pnlPlayer3.isVisible() != state) {
                pnlPlayer3.setVisible(state);
            }
        }
        if (n == 4) {
            if (pnlPlayer4.isVisible() != state) {
                pnlPlayer4.setVisible(state);
            }
        }
    }

    public void updateOptionPlayer(CustomizeData cData) {
        this.cData = cData;
        String[] playerName = cData.playerNames;
        if (playerName[0] != null) {
            hideShowChessMan(1, true);
            txtPlayerName1.setText(playerName[0]);
        }
        if (playerName[1] != null) {
            hideShowChessMan(2, true);
            txtPlayerName2.setText(playerName[1]);
        }
        if (playerName[2] != null) {
            hideShowChessMan(3, true);
            txtPlayerName3.setText(playerName[2]);
        }
        if (playerName[3] != null) {
            hideShowChessMan(4, true);
            txtPlayerName4.setText(playerName[3]);
        }
        txtOptionPlayer.setText(cData.getNumberOfPlayer() + "");


    }

    public void updatePriorityPerson() {
        switch (cData.getPriorityPerson()) {
            case 1:
                radPriority1.setSelected(true);
                break;
            case 2:
                radPriority2.setSelected(true);
                break;
            case 3:
                radPriority3.setSelected(true);
                break;
            case 4:
                radPriority3.setSelected(true);
                break;
        }
    }

    public void setDice() {
        // Update new CData receive from server
        int numOfDice = cData.getNumberOfDice();
        guiDice.setNumberOfDice(cData.getNumberOfDice());
        guiDice.localizeDice();
        if (numOfDice == 2) {
            radTwoDice.setSelected(true);
        } else {
            radOneDice.setSelected(true);
        }
    }

    public void updatePlayerInfo(PlayerPacket player) {
        int playerPos = player.getPosOfPlayer() + 1;
        System.out.println("Player position: " + playerPos);

        txtOptionPlayer.setText(cData.getNumberOfPlayer() + "");
        if (playerPos == 1) {
            hideShowChessMan(1, true);
            txtPlayerName1.setText(player.getPlayerName());
            lblNoOfHorse1.setText(player.getNumberOfChessman() + "");
        }
        if (playerPos == 2) {
            hideShowChessMan(2, true);
            txtPlayerName2.setText(player.getPlayerName());
            lblNoOfHorse2.setText(player.getNumberOfChessman() + "");
        }
        if (playerPos == 3) {
            hideShowChessMan(3, true);
            txtPlayerName3.setText(player.getPlayerName());
            lblNoOfHorse3.setText(player.getNumberOfChessman() + "");
        }
        if (playerPos == 4) {
            hideShowChessMan(4, true);
            txtPlayerName4.setText(player.getPlayerName());
            lblNoOfHorse4.setText(player.getNumberOfChessman() + "");
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btgDiceChosen;
    public javax.swing.JButton btnConnect;
    private javax.swing.JButton btnDisconnect;
    private javax.swing.JButton btnJoin;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnSend;
    private javax.swing.JComboBox cboRoom;
    private javax.swing.ButtonGroup grpPriority;
    private horsechess.client.gui.GUIDice guiDice;
    public static javax.swing.JList incommingList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    public static javax.swing.JLabel lblMessage;
    private javax.swing.JLabel lblNoOfHorse1;
    private javax.swing.JLabel lblNoOfHorse2;
    private javax.swing.JLabel lblNoOfHorse3;
    private javax.swing.JLabel lblNoOfHorse4;
    private javax.swing.JLabel lblPlayer1;
    private javax.swing.JLabel lblPlayer2;
    private javax.swing.JLabel lblPlayer3;
    private javax.swing.JLabel lblPlayer4;
    private javax.swing.JLabel lblServerIP1;
    private javax.swing.JLabel lblServerIP2;
    private javax.swing.JLabel lblServerIP3;
    private javax.swing.JLabel lblServerIP4;
    public javax.swing.JTabbedPane optionTab;
    private javax.swing.JLabel picChessman1;
    private javax.swing.JLabel picChessman2;
    private javax.swing.JLabel picChessman3;
    private javax.swing.JLabel picChessman4;
    private javax.swing.JPanel pnlChat;
    private javax.swing.JPanel pnlConnect;
    private javax.swing.JPanel pnlDice;
    private javax.swing.JPanel pnlPlayer;
    private javax.swing.JPanel pnlPlayer1;
    private javax.swing.JPanel pnlPlayer2;
    private javax.swing.JPanel pnlPlayer3;
    private javax.swing.JPanel pnlPlayer4;
    private javax.swing.JRadioButton radOneDice;
    private javax.swing.JRadioButton radPriority1;
    private javax.swing.JRadioButton radPriority2;
    private javax.swing.JRadioButton radPriority3;
    private javax.swing.JRadioButton radPriority4;
    private javax.swing.JRadioButton radTwoDice;
    private static javax.swing.JScrollPane scrollPane;
    private javax.swing.JTextArea txtMessage;
    public static javax.swing.JTextField txtOptionPlayer;
    private javax.swing.JTextField txtPlayerName;
    private javax.swing.JTextField txtPlayerName1;
    private javax.swing.JTextField txtPlayerName2;
    private javax.swing.JTextField txtPlayerName3;
    private javax.swing.JTextField txtPlayerName4;
    private javax.swing.JTextField txtServerIP;
    private javax.swing.JTextField txtServerPort;
    // End of variables declaration//GEN-END:variables
    public CustomizeData cData;
    private boolean connect = false;
    private PlayerConnect player;
    private ServerInfo serverInfo;
    public Timer t;
    public ArrayList<Thread> threads = new ArrayList<Thread>();
}
